﻿/*************************************************************
 * Software of blackcore
 *
 * CLRVersion:4.0.30319.586
 * MachineName:SPACE
 * Author:Andy Huang
 * Email:itblackhole@gmail.com
 * DateTime：6/26/2013 2:47:28 PM
 * Function：
 *************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Text;

namespace SobFS.Web.backend.manage.permission
{
    public partial class List : SobFS.Web.UI.BackendPage
    {
        #region ******************************Private Property******************************
        #endregion ******************************Private Property******************************


        #region ******************************Public Property******************************
        #endregion ******************************Public Property******************************


        #region ******************************Structure******************************
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindUserGroup();
            }
        }
        #endregion  ******************************Structure******************************


        #region ******************************Private Method******************************
        void BindUserGroup()
        {
            this.ddlUserGroup.DataSource = new BLL.SysGroup().GetList("IsObsoleted = 0");
            this.ddlUserGroup.DataTextField = "Name";
            this.ddlUserGroup.DataValueField = "Id";
            this.ddlUserGroup.DataBind();

            ListItem litem = new ListItem();
            litem.Text = "请选择分组";
            litem.Value = "0";
            this.ddlUserGroup.Items.Insert(0, litem);
        }

        void BindFunctionMenu()
        {
            this.gvList.DataSource = new BLL.SysFunctionMenu().GetList("IsObsoleted = 0", "Position ASC");
            this.gvList.DataBind();
        }


        void GetPermissionByUserGroupId(int userGroupId)
        {
            StringBuilder strPermission = new StringBuilder();
            try
            {
                BLL.SysPermission bllPermission = new BLL.SysPermission();
                List<Model.SysPermission> modelPermissions = bllPermission.GetModelList(string.Format("GroupId = {0}", userGroupId));
                if (modelPermissions != null && modelPermissions.Count > 0)
                {
                    foreach (var item in modelPermissions)
                    {
                        if (item == null) continue;
                        strPermission.Append(item.FunctionMenuId).Append(item.Permission);
                    }
                }
            }
            catch (Exception err)
            {
                JsMessageBox("绑定用户组权限信息失败：" + err.Message, string.Empty, UI.JsMessageBoxCss.Error);
            }
            finally
            {
                HtmlInputCheckBox chk;
                for (int i = 0; i < this.gvList.Rows.Count; i++)
                {
                    chk = (HtmlInputCheckBox)this.gvList.Rows[i].FindControl("chkV");
                    if (strPermission.ToString().IndexOf("," + chk.Value + ",") > -1) chk.Checked = true;
                    chk = (HtmlInputCheckBox)this.gvList.Rows[i].FindControl("chkA");
                    if (strPermission.ToString().IndexOf("," + chk.Value + ",") > -1) chk.Checked = true;
                    chk = (HtmlInputCheckBox)this.gvList.Rows[i].FindControl("chkE");
                    if (strPermission.ToString().IndexOf("," + chk.Value + ",") > -1) chk.Checked = true;
                    chk = (HtmlInputCheckBox)this.gvList.Rows[i].FindControl("chkD");
                    if (strPermission.ToString().IndexOf("," + chk.Value + ",") > -1) chk.Checked = true;
                }
            }
        }


        bool SavePermission(int userGroupId)
        {
            bool flag = false;
            HtmlInputCheckBox chk;
            for (int i = 0; i < this.gvList.Rows.Count; i++)
            {
                string strPermission = ",";

                chk = (HtmlInputCheckBox)this.gvList.Rows[i].FindControl("chkV");
                if (chk.Checked) strPermission += chk.Value + ",";
                chk = (HtmlInputCheckBox)this.gvList.Rows[i].FindControl("chkA");
                if (chk.Checked) strPermission += chk.Value + ",";
                chk = (HtmlInputCheckBox)this.gvList.Rows[i].FindControl("chkE");
                if (chk.Checked) strPermission += chk.Value + ",";
                chk = (HtmlInputCheckBox)this.gvList.Rows[i].FindControl("chkD");
                if (chk.Checked) strPermission += chk.Value + ",";

                int functionMenuId = Convert.ToInt32(this.gvList.DataKeys[i].Value);
                if (functionMenuId > 0)
                {
                    if (AddOrUpdate(userGroupId, functionMenuId, strPermission))
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
            }
            return flag;
        }

        bool AddOrUpdate(int userGroupId, int functionMenuId, string strPermission)
        {
            try
            {
                BLL.SysPermission bllPermission = new BLL.SysPermission();
                List<Model.SysPermission> modelPermissions = bllPermission.GetModelList(string.Format("GroupId = {0} AND FunctionMenuId = {1}", userGroupId, functionMenuId));
                if (modelPermissions != null && modelPermissions.Count > 0)
                {
                    foreach (var item in modelPermissions)
                    {
                        if (item == null) continue;
                        item.Permission = strPermission;
                        bllPermission.Update(item);
                    }
                }
                else
                {
                    if (strPermission.IndexOf('|') >= 0)
                    {
                        bllPermission.Add(new Model.SysPermission()
                        {
                            GroupId = userGroupId,
                            FunctionMenuId = functionMenuId,
                            Permission = strPermission
                        });
                    }
                }
                return true;
            }
            catch (Exception err)
            {
                JsMessageBox("操作失败：" + err.Message, string.Empty, UI.JsMessageBoxCss.Error);
                return false;
            }
        }
        #endregion ******************************Private Method******************************






        #region ******************************Public Method******************************
        public bool GetPermission(string strPermission)
        {
            if (string.IsNullOrWhiteSpace(strPermission)) return false;
            if (strPermission.IndexOf('|') > -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected void ddlUserGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindFunctionMenu();
            int userGroupId = Convert.ToInt32(this.ddlUserGroup.SelectedItem.Value);
            GetPermissionByUserGroupId(userGroupId);
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            HtmlInputCheckBox cb = (HtmlInputCheckBox)e.Row.FindControl("chkV");
            Label lblParent = (Label)e.Row.FindControl("lblFunctionMenu");
            if (cb == null) return;

            //父级菜单加粗显示
            if (cb.Visible == false)
            {
                e.Row.BackColor = Color.AliceBlue;
                if (lblParent != null)
                {
                    e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
                    lblParent.Font.Bold = true;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int userGroupId = Convert.ToInt32(this.ddlUserGroup.SelectedItem.Value);
            if (userGroupId > 0)
            {
                if (SavePermission(userGroupId))
                {
                    JsMessageBox("操作成功！", string.Empty, UI.JsMessageBoxCss.Success);
                }
                else
                {
                    JsMessageBox("操作失败！", string.Empty, UI.JsMessageBoxCss.Error);
                }
            }
            else
            {
                JsMessageBox("请选择用户组......", string.Empty, UI.JsMessageBoxCss.Error);
            }
        }
        #endregion ******************************Public Method******************************




    }
}